import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/hj_page/cartoon_business/cartoon_sub_detail_page.dart';
import 'package:xvideos/tools_base/indicator/custom_tab_indicator.dart';

import 'cartoon_sub_module_logic.dart';

enum CommunityType {
  Pic, // 图集
  Game, // 黄游
  Novel, // 小说
  Cartoon, //动漫和漫画
}

//漫画亚模块
class CartoonSubModulePage extends StatefulWidget {
  final CommunityType type; //-1:动漫和漫画 0:图集 1:黄游 2:小说
  final int tabIndex;

  const CartoonSubModulePage({super.key, this.type = CommunityType.Pic, this.tabIndex = 0});

  @override
  State<CartoonSubModulePage> createState() => _CartoonSubModulePageState();
}

class _CartoonSubModulePageState extends State<CartoonSubModulePage> {
  late CartoonSubModuleLogic logic;

  @override
  void initState() {
    super.initState();
    logic = CartoonSubModuleLogic(widget.tabIndex, type: widget.type);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CartoonSubModuleLogic>(
      init: logic,
      tag: widget.type.toString(),
      builder: (_) => Column(
        children: [
          _buildTabbar(),
          Expanded(
            child: TabBarView(
              controller: _.tabController,
              children: List.generate(
                _.tabs.length,
                (index) => CartoonSubDetailPage(
                  tagData: _.tabs[index],
                  type: widget.type,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildTabbar() {
    return SizedBox(
      height: 44,
      child: TabBar(
        controller: logic.tabController,
        padding: EdgeInsets.symmetric(horizontal: 8),
        tabs: List.generate(
          logic.tabs.length,
          (index) => Padding(
            padding: const EdgeInsets.only(top: 6, bottom: 8),
            child: Text(logic.tabs[index].moduleName ?? ''),
          ),
        ),
        labelPadding: EdgeInsets.symmetric(horizontal: 8),
        tabAlignment: TabAlignment.start,
        isScrollable: true,
        labelColor: Colors.white.withOpacity(.9),
        labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
        unselectedLabelColor: Colors.white.withOpacity(.35),
        unselectedLabelStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
        indicator: CustomIndicator(
          width: 16,
          height: 4,
          topLeft: 3,
          topRight: 3,
          bottomLeft: 0,
          bottomRight: 0,
          color: AppColors.primaryTextColor,
        ),
      ),
    );
  }
}
